finp$="picture.png"

xed=256:yed=192
dim celcnt[256]
dim attartd[32,32,2]
dim celcnb[256]
dim pacm[256],ikcm[256]

function grayscaleindexed(c)
  ct=c
  bq=bitwiseand(ct,0x03)*2:ct=int(ct/4)
  rq=bitwiseand(ct,0x07)  :ct=int(ct/8)
  gq=bitwiseand(ct,0x07)
  return int(  ((bq*11)+(rq*30)+(gq*59))/100 )
  end function

setdisplay(xed,yed,32,1):paper(8^8-1):ink(0):pen(0):cls
loadimage(finp$,1):pasteicon(0,0,1)

for i=0 to 255:celcnt[i]=0:next

print
print "-------------------------"
print "index colours from attributes"
print

for y1=0 to yed-1 step 8
  for x1=0 to xed-1 step 8
    for i=0 to 255:celcnt[i]=0:next
    for y2=0 to 7
      for x2=0 to 7
        x=x1+x2:y=y1+y2
        u1=point(x,y)
        b=bitwiseand(u1,0xC0)/64:u1=int(u1/256)
        g=bitwiseand(u1,0xE0)/32:u1=int(u1/256)
        r=bitwiseand(u1,0xE0)/32
        u=(b)+(r*4)+(g*32)
        'print u : '-?
        celcnt[u]=celcnt[u]+1
        next:next

    ikattr=0:paattr=0:ikcnt=0:pacnt=0
    for i=0 to 255:if ikcnt<celcnt[i] then: ikcnt=celcnt[i]:ikattr=i:end if:next
    celcnt[ikattr]=0
    for i=0 to 255:if pacnt<celcnt[i] then: pacnt=celcnt[i]:paattr=i:end if:next

    txou$=str$(y1/8)+","+str$(x1/8)+":"+str$(ikattr)+","+str$(paattr)
    print txou$

    if grayscaleindexed(ikattr)>grayscaleindexed(paattr) then:tmpr=ikattr:ikattr=paattr:paattr=tmpr:end if

    if ikattr=paattr then:
      ikf=0
      if ikattr<128 then:ikf=255:end if
      ikattr=ikf
      end if
    
    wmid=(grayscaleindexed(paattr)+grayscaleindexed(ikattr))/2

    attartd[int(x1/8),int(y1/8),0]=ikattr
    attartd[int(x1/8),int(y1/8),1]=paattr

    for y2=0 to 7
      for x2=0 to 7
        x=x1+x2:y=y1+y2
        u1=point(x,y)
        b=bitwiseand(u1,0xC0)/64:u1=int(u1/256)
        g=bitwiseand(u1,0xE0)/32:u1=int(u1/256)
        r=bitwiseand(u1,0xE0)/32
        u=(b)+(r*4)+(g*32)
        if u=ikattr then:idou=ikattr:end if
        if u<>ikattr then:
          if u=paattr then:idou=paattr:end if
          if u<>paattr then:
            idou=ikattr
            if grayscaleindexed(u)>wmid then:idou=paattr:end if
            end if
          ink(rgb( bitwiseand(idou,0x1C)*8,   bitwiseand(idou,0xE0)   ,   bitwiseand(idou,0x03)*64   ))   
          dot(x,y)

          end if
        next:next




    next:next

'- find most user colour from attributes
print
print "-------------------------"
print "index colour ammounts from all attributes "
print

for i=0 to 255:celcnb[i]=0:next

for y1=0 to 23:for x1=0 to 31:for sb=0 to 1
  atrv=attartd[x1,y1,sb]
  celcnb[atrv]=celcnb[atrv]+1
  next:next:next


mxv=0:idv=0
for i=0 to 255:
  print i;":";celcnb[i]
  if celcnb[i] > mxv then:mxv=celcnb[i]:idv=i:end if
  print "mxv:";mxv
  print "idv:";idv
  next
print

cclcnt=0:cclvl=0
for i=0 to 255:
  celt=celcnb[i]
  if celt>cclcnt then:
    cclcnt=celt
    cclvl=i
    print "new index with max cells:";i	
    end if
  next


print
print "most common colour:";cclvl

celcnb[cclvl]=0


'----
print
print "-------------------------"
print "8 cores..."
print

for j=0 to 7
  for i=0 to 255:celcnb[i]=0:next
  for y1=0 to 23:for x1=0 to 31
    atrv0=attartd[x1,y1,0]
    atrv1=attartd[x1,y1,1]
    if atrv0=cclvl then:celcnb[atrv1]=celcnb[atrv1]+1:end if
    if atrv1=cclvl then:celcnb[atrv0]=celcnb[atrv0]+1:end if
    next:next

  mxv=0:idv=0
  for i=0 to 255:
    print i;":";celcnb[i]
    if celcnb[i] > mxv then:mxv=celcnb[i]:idv=i:end if
    print "mxv:";mxv
    print "idv:";idv
    next

  ikcm[j]=idv
  celcnb[ikcm[j]]=0
  next

for i=0 to 7
  print ikcm[i]:next


open "_qqq.csv" for output as #1
   for y=0 to 23
     qqq$=""
     for x=0 to 31
       qqq$=qqq$+"_"+ucase$(right$("00"+hex$(attartd[x,y,0]),2))+","
       next
     print #1,qqq$
     next
   for y=0 to 23
     qqq$=""
     for x=0 to 31
       qqq$=qqq$+"_"+ucase$(right$("00"+hex$(attartd[x,y,1]),2))+","
       next
     print #1,qqq$
     next
   close #1

shell("gnumeric _qqq.csv")


  
waitkey














